unit UDevCtrlMgr;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, USingleGridBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxStyles, dxSkinsCore,
  dxSkinOffice2016Colorful, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit,
  cxNavigator, cxDataControllerConditionalFormattingRulesManagerDialog, Data.DB,
  cxDBData, MemDS, DBAccess, Uni, System.ImageList, Vcl.ImgList, cxImageList, dxBar,
  cxClasses, cxGridLevel, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, UCommonConst;

type
  TfrmDevCtrlMgr = class(TfrmSingleGridBase)
    grdMainDBTableView1Column1: TcxGridDBColumn;
    grdMainDBTableView1Column2: TcxGridDBColumn;
    grdMainDBTableView1Column3: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure grdMainDBTableView1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure btnDeleteClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    procedure ShowAllData;
    procedure RefeshALLData(var message: TMessage); message WM_REFRESHDATA;
  end;

var
  frmDevCtrlMgr: TfrmDevCtrlMgr;

implementation

uses
  UCommonFun, UDevCtrlAdd, UDevCtrlEdit, UCommonFunSqlite;

{$R *.dfm}

procedure TfrmDevCtrlMgr.btnAddClick(Sender: TObject);
begin
  inherited;

  if Application.FindComponent('frmDevCtrlAdd') = nil then
  begin
    frmDevCtrlAdd := TfrmDevCtrlAdd.Create(Application);
    try
      frmDevCtrlAdd.ParentHandle := Handle;
      frmDevCtrlAdd.Show;
    except
    end;
  end
  else
  try
    frmDevCtrlAdd.BringToFront;
  except
  end;
end;

procedure TfrmDevCtrlMgr.btnDeleteClick(Sender: TObject);
var
  sSql: string;
  iCtrlID: Integer;
begin
  inherited;

  if (not qryMain.Active) or (qryMain.Eof) then
  begin
    MsgBoxDefault(RESNOTDELETEDATA);
    Abort;
  end;

  with qryMain do
  begin
    if Active then
      iCtrlID := FieldByName('CtrlID').AsInteger
    else
      Exit;
  end;

  if MsgYesNoDefault(RESDBDELETECONFIRM) then
  begin
    sSql := 'delete from DevCtrlData where CtrlID=''' + IntToStr(iCtrlID) + '''';
    if ExecSql(sSql) then
    begin
      SendMessage(Handle, WM_REFRESHDATA, 0, 0);
    end
    else
      MsgBoxDefault(RESDBDELETEFAIL);
  end;
end;

procedure TfrmDevCtrlMgr.btnEditClick(Sender: TObject);
var
  iSelectCtrlID: Integer;
begin
  inherited;

  if (not qryMain.Active) or (qryMain.Eof) then
  begin
    MsgBoxDefault(RESNOTEDITDATA);
    Abort;
  end;

  iSelectCtrlID := qryMain.FieldByName('CtrlID').AsInteger;

  frmDevCtrlEdit := TfrmDevCtrlEdit.Create(Application);
  try
    frmDevCtrlEdit.SelectID := iSelectCtrlID;
    frmDevCtrlEdit.ParentHandle := Handle;
    frmDevCtrlEdit.ShowModal;
  finally
    frmDevCtrlEdit.Free;
  end;
end;

procedure TfrmDevCtrlMgr.FormCreate(Sender: TObject);
begin
  inherited;

  ShowAllData;
end;

procedure TfrmDevCtrlMgr.grdMainDBTableView1CellDblClick(
  Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo;
  AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;

  btnEditClick(Self);
end;

procedure TfrmDevCtrlMgr.ShowAllData;
var
  sSql: string;
begin
  sSql := 'select a.CtrlID,a.CtrlName,a.CtrlIP,(select ThemeName from ThemeData where ThemeID=a.ThemeID limit 1) ThemeCN from DevCtrlData a order by a.CtrlID';

  with qryMain do
  begin
    Close;
    SQL.Text := sSql;
    Open;
  end;
end;

procedure TfrmDevCtrlMgr.RefeshALLData(var message: TMessage);
begin
  ShowAllData;
end;

end.
